package com.faker.dao;


import com.faker.entity.SysConnection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
@Slf4j
public class ConnectionManagerDao {

    @Resource
    JdbcTemplate jdbcTemplate;


    public SysConnection findConnectionById(Long connectionId) {
        BeanPropertyRowMapper<SysConnection> mapper = new BeanPropertyRowMapper<>(SysConnection.class);
        StringBuffer sb = new StringBuffer();
        sb.append("  select connection_id as connectionId,     ");
        sb.append("         name          as name,             ");
        sb.append("         url           as url,              ");
        sb.append("         username      as username,         ");
        sb.append("         password      as password          ");
        sb.append("  from sys_connection                       ");
        sb.append("  where connection_id = ?                   ");
        SysConnection sysConnection = jdbcTemplate.queryForObject(sb.toString(), mapper, connectionId);
        return sysConnection;
    }

    public List<SysConnection> getConnectionList() {
        BeanPropertyRowMapper<SysConnection> mapper = new BeanPropertyRowMapper<>(SysConnection.class);
        StringBuffer sb = new StringBuffer();
        sb.append(" select connection_id as connectionId,   ");
        sb.append("        name          as name,           ");
        sb.append("        url           as url,            ");
        sb.append("        username      as username,       ");
        sb.append("        password      as password        ");
        sb.append(" from sys_connection                     ");
        List<SysConnection> list = jdbcTemplate.query(sb.toString(), mapper);
        return list;
    }


    public int insertConnection(SysConnection sysConnection) {
        StringBuffer sb = new StringBuffer();
        sb.append(" insert into sys_connection (name, url, username, password )    ");
        sb.append(" values (?, ?, ?, ?)                                            ");
        int update = jdbcTemplate.update(sb.toString(), sysConnection.getName(), sysConnection.getUrl(), sysConnection.getUsername(), sysConnection.getPassword());
        return update;
    }

    public int updateConnection(SysConnection sysConnection) {
        StringBuffer sb = new StringBuffer();
        sb.append(" update sys_connection   ");
        sb.append(" set name   = ?,     ");
        sb.append("     url = ?,        ");
        sb.append("     username = ?,   ");
        sb.append("     password = ?    ");
        sb.append(" where connection_id = ?        ");
        int update = jdbcTemplate.update(sb.toString(), sysConnection.getName(), sysConnection.getUrl(), sysConnection.getUsername(), sysConnection.getPassword(), sysConnection.getConnectionId());
        return update;
    }


    public int deleteConnection(Long id) {
        StringBuffer sb = new StringBuffer();
        sb.append(" delete             ");
        sb.append(" from sys_connection    ");
        sb.append(" where connection_id = ?       ");
        int update = jdbcTemplate.update(sb.toString(), id);
        return update;
    }

}
